package com.cjj.refresh;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

/**
 * cjj
 */
public class RippleView extends Component implements Component.DrawTask {
    private static final int INVALIDATE_CODE = 9;
    private Paint mPaint;
    private int radius;
    private RippleListener listener;
    private final MyHandler eventHandler = new MyHandler(EventRunner.getMainEventRunner());

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public RippleView(Context context) {
        this(context, null, 0);
    }

    public RippleView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public RippleView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setAlpha(0.5f);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        addDrawTask(this);
    }

    /**
     * startReveal
     */
    public void startReveal() {
        new Thread(() -> {
            int bigRadius = (int) (Math.sqrt(Math.pow(getHeight(), 2) + Math.pow(getWidth(), 2)));
            while (radius < bigRadius) {
                radius += 30;
                eventHandler.sendEvent(INVALIDATE_CODE);
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            radius = 0;
            eventHandler.sendEvent(INVALIDATE_CODE);
            if (listener != null) {
                listener.onRippleFinish();
            }
        }).start();
    }

    private class MyHandler extends EventHandler {
        MyHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event.eventId == INVALIDATE_CODE) {
                invalidate();
            }
        }
    }

    /**
     * setRippleListener
     *
     * @param listener listener
     */
    public void setRippleListener(RippleListener listener) {
        this.listener = listener;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawCircle((float) ((double) getWidth() / 2), (float) ((double) getHeight() / 2), radius, mPaint);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        onDraw(this, mCanvasForTaskOverContent);
    }

    /**
     * RippleListener
     */
    public interface RippleListener {
        void onRippleFinish();
    }
}